'use strict';
/**
 * Clip-Two Main Controller
 */
app.controller('AppCtrl', ['$rootScope', '$scope', '$state', '$translate', '$localStorage', '$window', '$document', '$timeout', '$http', 'cfpLoadingBar', 'cookieUtil',
    function($rootScope, $scope, $state, $translate, $localStorage, $window, $document, $timeout, $http, cfpLoadingBar, cookieUtil) {

        // Loading bar transition
        // -----------------------------------
        var $win = $($window),
            FORBIDDEN_CLASS_KEYS = ['Detail', 'edit', 'Edite', 'Editor', 'Mdify', 'Medify', 'Add'];

        $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
            // start loading bar on stateChangeStart
            // if (cookieUtil.getCookie('token')=='') {
            //     $state.go('/login')
            // }
            if (toState.name != "login") {
                if (cookieUtil.getCookie('token')=='') {
                    $state.go('login');
                    event.preventDefault();
                } else {
                    cfpLoadingBar.start();
                }
            }else{
                cfpLoadingBar.start();
            }

        });
        $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {

            //stop loading bar on stateChangeSuccess
            event.targetScope.$watch("$viewContentLoaded", function() {

                cfpLoadingBar.complete();
            });

            // scroll top the page on change state

            $document.scrollTo(0, 0);

            if (angular.element('.email-reader').length) {
                angular.element('.email-reader').animate({
                    scrollTop: 0
                }, 0);
            }
            // Save the route title
            $rootScope.currTitle = $state.current.title;

            //设置菜单相关样式的url
            resetClassUrl();
        });

        // State not found
        $rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams) {
            //$rootScope.loading = false;
            console.log(unfoundState.to);
            // "lazy.state"
            console.log(unfoundState.toParams);
            // {a:1, b:2}
            console.log(unfoundState.options);
            // {inherit:false} + default options
        });

        $rootScope.pageTitle = function() {
            return $rootScope.app.name; // + ' - ' + ($rootScope.currTitle || $rootScope.app.description);
        };

        // save settings to local storage
        if (angular.isDefined($localStorage.layout)) {
            $scope.app.layout = $localStorage.layout;

        } else {
            $localStorage.layout = $scope.app.layout;
        }
        $scope.$watch('app.layout', function() {
            // save to local storage
            $localStorage.layout = $scope.app.layout;
        }, true);

        //global function to scroll page up
        $scope.toTheTop = function() {

            $document.scrollTopAnimated(0, 600);

        };

        // angular translate
        // ----------------------

        $scope.language = {
            // Handles language dropdown
            listIsOpen: false,
            // list of available languages
            available: {
                'en': 'English',
                'it_IT': 'Italiano',
                'de_DE': 'Deutsch'
            },
            // display always the current ui language
            init: function() {
                var proposedLanguage = $translate.proposedLanguage() || $translate.use();
                var preferredLanguage = $translate.preferredLanguage();
                // we know we have set a preferred one in app.config
                $scope.language.selected = $scope.language.available[(proposedLanguage || preferredLanguage)];
            },
            set: function(localeId, ev) {
                $translate.use(localeId);
                $scope.language.selected = $scope.language.available[localeId];
                $scope.language.listIsOpen = !$scope.language.listIsOpen;
            }
        };

        $scope.language.init();

        // Function that find the exact height and width of the viewport in a cross-browser way
        var viewport = function() {
            var e = window,
                a = 'inner';
            if (!('innerWidth' in window)) {
                a = 'client';
                e = document.documentElement || document.body;
            }
            return {
                width: e[a + 'Width'],
                height: e[a + 'Height']
            };
        };
        // function that adds information in a scope of the height and width of the page
        $scope.getWindowDimensions = function() {
            return {
                'h': viewport().height,
                'w': viewport().width
            };
        };
        // Detect when window is resized and set some variables
        $scope.$watch($scope.getWindowDimensions, function(newValue, oldValue) {
            $scope.windowHeight = newValue.h;
            $scope.windowWidth = newValue.w;
            if (newValue.w >= 992) {
                $scope.isLargeDevice = true;
            } else {
                $scope.isLargeDevice = false;
            }
            if (newValue.w < 992) {
                $scope.isSmallDevice = true;
            } else {
                $scope.isSmallDevice = false;
            }
            if (newValue.w <= 768) {
                $scope.isMobileDevice = true;
            } else {
                $scope.isMobileDevice = false;
            }
        }, true);
        // Apply on resize
        $win.on('resize', function() {
            $scope.$apply();
        });

        $scope.currentUser = null;
        // $scope.isAuthorized = AuthService.isAuthorized;
        $scope.classUrl = '';

        function resetClassUrl(){
            storePreclassUrl();
            setClassUrl();
        }

        function storePreclassUrl() {
            var preclassUrl = '',
                currentState = $state.current;

            if (isValidCurrentState()) {
                window.sessionStorage.setItem('classUrl', currentState.url);
            }
        }

        function setClassUrl() {
            $scope.classUrl = window.sessionStorage.getItem('classUrl');
        }

        function isValidCurrentState() {
            var currentState = $state.current;

            for (var i = 0; i < FORBIDDEN_CLASS_KEYS.length; i++) {
                if (currentState.url.indexOf(FORBIDDEN_CLASS_KEYS[i]) >= 0) {
                    return false;
                }
            }

            if (!currentState.name) {
                return false;
            }

            return true;
        }


    }
]);
